<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/**
 * Product view template
 *
 * @see Mage_Catalog_Block_Product_View
 * @see Mage_Review_Block_Product_View
 */
?>
<?php
$_helper = $this->helper('catalog/output');
$_product = $this->getProduct();
$enable_upsell =  Mage::getStoreConfig('shoppersettings/labels/enable_upsell');
$replace_upsell =  Mage::getStoreConfig('shoppersettings/labels/replace_upsell');

$helper = $this->helper('shoppersettings/image');
list($defaultImgX, $defaultImgY) = $helper->getDefaultSize();
list($imgX, $imgY) = $helper->getMainSize();
if ( $imgX > 800 ) {
    $imgX = 800;
    $imgY = $helper->calculateHeight($imgX);
}

$product_img_box_width = $defaultImgX;
if ( $defaultImgX != $imgX ) {
    $product_img_box_width = $imgX;
}
//add image border
$product_img_box_width += 18;
?>
<?php
if (!$_category = Mage::registry('current_category')) {
    $_product = Mage::getSingleton( 'catalog/product' )->load( $_product->getId() );
    $categories=$_product->getCategoryIds();
    $_category = Mage::getModel('catalog/category')->load($categories[0]);
}
if ($_category) {
echo '<div class="product-category-title">'.$_category->getName().'</div>';
}
 ?>

<script type="text/javascript">
    var optionsPrice = new Product.OptionsPrice(<?php echo $this->getJsonConfig() ?>);
</script>
<?php echo $this->getChildHtml('global_messages') ?>
<div id="messages_product_view"><?php echo $this->getMessagesBlock()->getGroupedHtml() ?></div>
<div class="product-view">
    <div class="product-essential">
    <form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
        <div class="no-display">
            <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
            <input type="hidden" name="related_product" id="related-products-field" value="" />
        </div>

        <div class="product-img-box" style="width:<?php echo $product_img_box_width; ?>px">
            <?php echo $this->helper('shoppersettings')->getLabel($_product);  ?>
            <?php echo $this->getChildHtml('media') ?>
        </div>

        <div class="product-shop">
            <?php
            $output = '';
            switch ($replace_upsell) {
                case 'always':
                    $output = $this->getChildHtml('queldorei_product_additional');
                    break;
                case 'never':
                    if ($enable_upsell) $output = $this->getChildHtml('upsell_products');
                    break;
                case 'only':
                    if ($enable_upsell) $output = $this->getChildHtml('upsell_products');
                    if ( empty($output) ) $output = $this->getChildHtml('queldorei_product_additional');
                    break;
            }
            if(!empty($output)){
                echo '<div class="product-additional">'.$output.'</div>';
            }
            ?>
            <div class="product-shop-info">
                <div class="product-name">
                    <h1><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h1>
                </div>
                <?php echo $this->getReviewsSummaryHtml($_product, false, true)?>

                <?php echo $this->getChildHtml('alert_urls') ?>
                <?php echo $this->getChildHtml('product_type_data') ?>
                <?php echo $this->getTierPriceHtml() ?>
                <?php echo $this->getChildHtml('extrahint') ?>

                <?php if ($_product->getShortDescription()):?>
                <div class="short-description">
                    <div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
                </div>
                <?php endif;?>

                <?php echo $this->getChildHtml('other');?>

                <?php if(!$_product->isGrouped()): ?>
                <div class="qty-container clearfix">
                    <label for="qty"><?php echo $this->__('Qty') ?>:</label>
                    <input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getProductDefaultQty() * 1 ?>" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
                </div>
                <?php endif; ?>

                <?php if (!$this->hasOptions()):?>
                    <div class="add-to-box">
                        <?php if($_product->isSaleable()): ?>
                            <?php echo $this->getChildHtml('addtocart') ?>
                        <?php endif; ?>
                    </div>
                    <?php echo $this->getChildHtml('extra_buttons') ?>
                <?php endif; ?>

                <?php if ($_product->isSaleable() && $this->hasOptions()):?>
                    <?php echo $this->getChildChildHtml('container1', '', true, true) ?>
                <?php endif;?>

                <?php echo $this->getChildHtml('addto') ?>
                <div class="clear"></div>
                <?php echo $this->getChildHtml('shopper_product_addthis') ?>
            </div>
        </div>
        <div class="clearer"></div>
	    <?php if ($_product->isSaleable() && $this->hasOptions()):?>
	        <?php echo $this->getChildChildHtml('container2', '', true, true) ?>
	    <?php endif;?>
    </form>
    <script type="text/javascript">
    //<![CDATA[

		<?php if ( !Mage::getStoreConfig('shoppersettings/ajax/cart', Mage::app()->getStore()->getId())) : ?>

        var productAddToCartForm = new VarienForm('product_addtocart_form');
        productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                    form.action = url;
                }
                var e = null;
                try {
                    this.form.submit();
                } catch (e) {
                }
                this.form.action = oldUrl;
                if (e) {
                    throw e;
                }

                if (button && button != 'undefined') {
                    button.disabled = true;
                }
            }
        }.bind(productAddToCartForm);

        <?php else : ?>

        function setAjaxData(data,iframe){
            //showMessage(data.message);
            if (data.status != 'ERROR' && jQuery('.cart-top-container').length) {
                jQuery('.cart-top-container').replaceWith(data.cart_top);
            }
        }

        function showMessage(message)
        {
            jQuery('body').append('<div class="alert"></div>');
            var $alert = jQuery('.alert');
            $alert.slideDown(400);
            $alert.html(message).append('<button></button>');
            jQuery('button').click(function () {
                $alert.slideUp(400);
            });
            $alert.slideDown('400', function () {
                setTimeout(function () {
                    $alert.slideUp('400', function () {
                        jQuery(this).slideUp(400, function(){ jQuery(this).detach(); })
                    });
                }, 7000)
            });
        }

        var productAddToCartForm = new VarienForm('product_addtocart_form');
        productAddToCartForm.submit = function (button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;
                if (url) {
                    form.action = url;
                }
                var e = null;
                // Start of our new ajax code
                if (!url) {
                    url = jQuery('#product_addtocart_form').attr('action');
                }
                url = url.replace("checkout/cart", "ajax/index"); // New Code
                var data = jQuery('#product_addtocart_form').serialize();
                data += '&isAjax=1';
                jQuery('#ajax_loading'+ jQuery('#product_addtocart_form').find("[name='product']").val() ).css('display', 'block');
                try {
                    jQuery.ajax({
                        url:url,
                        dataType:'jsonp',
                        type:'post',
                        data:data,
                        success:function (data) {
                            jQuery('#ajax_loading'+ jQuery('#product_addtocart_form').find("[name='product']").val() ).css('display', 'none');
                            setAjaxData(data, true);
                            showMessage(data.message);
                        }
                    });
                } catch (e) {
                }
                // End of our new ajax code
                this.form.action = oldUrl;

                if (e) {
                    throw e;
                }
            }
        }.bind(productAddToCartForm);

        <?php endif; ?>

	    productAddToCartForm.submitLight = function(button, url){
	        if(this.validator) {
	            var nv = Validation.methods;
	            delete Validation.methods['required-entry'];
	            delete Validation.methods['validate-one-required'];
	            delete Validation.methods['validate-one-required-by-name'];
	            if (this.validator.validate()) {
	                if (url) {
	                    this.form.action = url;
	                }
	                this.form.submit();
	            }
	            Object.extend(Validation.methods, nv);
	        }
	    }.bind(productAddToCartForm);
    //]]>
    </script>
    </div>

	<!-- ADDITIONAL -->
	<?php echo $this->getChildHtml('product_additional_data') ?>
	<!-- ADDITIONAL -->

    <?php
    $output = '';
    switch ($replace_upsell) {
        case 'always':
            break;
        case 'never':
            if ($enable_upsell) $output = $this->getChildHtml('upsell_products2');
            break;
        case 'only':
            if ($enable_upsell) $output = $this->getChildHtml('upsell_products2');
            break;
    }
    if(!empty($output)){
        echo $output;
    }
    ?>

    <?php
    $after_tabs_block = Mage::getModel('cms/block')
	    ->setStoreId( Mage::app()->getStore()->getId() )
	    ->load('shopper_after_tabs');
    if($after_tabs_block->getIsActive()){
        echo '<div class="after-tabs-block additional-block">'.$this->getLayout()->createBlock('cms/block')->setBlockId('shopper_after_tabs')->toHtml().'</div>';
    }
    ?>

	<!-- RELATED -->
	<?php echo $this->getChildHtml('content_related') ?>
	<!-- RELATED -->

</div>
